package com.gorillalogic.monkeytalk.demo1;

import android.test.ActivityInstrumentationTestCase2;
import android.app.Activity;
import junit.framework.AssertionFailedError;

import com.bitbar.recorder.extensions.ExtSolo;

import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class NewRobotiumTest_demo1 extends
		ActivityInstrumentationTestCase2<Activity> {

	private static final String LAUNCHER_ACTIVITY_CLASSNAME = "com.gorillalogic.monkeytalk.demo1.RootActivity";
	private static Class<?> launchActivityClass;
	static {
		try {
			launchActivityClass = Class.forName(LAUNCHER_ACTIVITY_CLASSNAME);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
	}
	private ExtSolo solo; // ExtSolo is an extension of Robotium Solo that helps collecting better test execution data during test runs

	@SuppressWarnings("unchecked")
	public NewRobotiumTest_demo1() {
		super((Class<Activity>) launchActivityClass);
	}

	@Override
	public void setUp() throws Exception {
		super.setUp();
		solo = new ExtSolo(getInstrumentation(), getActivity(), this.getClass()
				.getCanonicalName(), getName());
	}

	@Override
	public void tearDown() throws Exception {
		solo.finishOpenedActivities();
		solo.tearDown();
		super.tearDown();
	}

	public void testRecorded() throws Exception {
		try {
			solo.waitForActivity("RootActivity");
			solo.waitForActivity("LoginActivity");
			solo.sleep(4400);
			assertTrue(
					"Wait for edit text (id: com.gorillalogic.monkeytalk.demo1.R.id.login_usr) failed.",
					solo.waitForEditTextById(
							"com.gorillalogic.monkeytalk.demo1.R.id.login_usr",
							20000));
			solo.enterText(
					(EditText) solo
							.findViewById("com.gorillalogic.monkeytalk.demo1.R.id.login_usr"),
					"ggggg");
			solo.sleep(1900);
			assertTrue(
					"Wait for edit text (id: com.gorillalogic.monkeytalk.demo1.R.id.login_pwd) failed.",
					solo.waitForEditTextById(
							"com.gorillalogic.monkeytalk.demo1.R.id.login_pwd",
							20000));
			solo.enterText(
					(EditText) solo
							.findViewById("com.gorillalogic.monkeytalk.demo1.R.id.login_pwd"),
					"ggggg");
			assertTrue(
					"Wait for button (id: com.gorillalogic.monkeytalk.demo1.R.id.login_btn) failed.",
					solo.waitForButtonById(
							"com.gorillalogic.monkeytalk.demo1.R.id.login_btn",
							20000));
			solo.clickOnButton((Button) solo
					.findViewById("com.gorillalogic.monkeytalk.demo1.R.id.login_btn"));
			solo.waitForActivity("LogoutActivity");
			solo.sleep(1800);
			assertTrue(
					"Wait for button (id: com.gorillalogic.monkeytalk.demo1.R.id.logout_btn) failed.",
					solo.waitForButtonById(
							"com.gorillalogic.monkeytalk.demo1.R.id.logout_btn",
							20000));
			solo.clickOnButton((Button) solo
					.findViewById("com.gorillalogic.monkeytalk.demo1.R.id.logout_btn"));
			solo.waitForActivity("RootActivity");
			solo.waitForActivity("LoginActivity");
			solo.sleep(1600);
			assertTrue("Wait for view (index: 8) failed.",
					solo.waitForView(8, 20000));
			solo.clickOnView(8);
			solo.waitForActivity("FormsActivity");
			solo.sleep(2200);
			assertTrue("Wait for spinner (index: 0) failed.",
					solo.waitForSpinner(0, 20000));
			solo.pressSpinnerItem(0, 3);
			solo.sleep(1100);
			solo.clickOnScreen(solo.toScreenX(0.510f), solo.toScreenY(0.572f));
			solo.sleep(1000);
			assertTrue(
					"Wait for radio button (id: com.gorillalogic.monkeytalk.demo1.R.id.forms_radio_c) failed.",
					solo.waitForRadioButtonById(
							"com.gorillalogic.monkeytalk.demo1.R.id.forms_radio_c",
							20000));
			solo.clickOnRadioButton((RadioButton) solo
					.findViewById("com.gorillalogic.monkeytalk.demo1.R.id.forms_radio_c"));
			assertTrue(
					"Wait for radio button (id: com.gorillalogic.monkeytalk.demo1.R.id.forms_radio_b) failed.",
					solo.waitForRadioButtonById(
							"com.gorillalogic.monkeytalk.demo1.R.id.forms_radio_b",
							20000));
			solo.clickOnRadioButton((RadioButton) solo
					.findViewById("com.gorillalogic.monkeytalk.demo1.R.id.forms_radio_b"));
			solo.sleep(1700);
			solo.goBack();
			solo.waitForActivity("RootActivity");
			solo.waitForActivity("LoginActivity");
			solo.sleep(1800);
			assertTrue("Wait for view (index: 11) failed.",
					solo.waitForView(11, 20000));
			solo.clickOnView(11);
			solo.waitForActivity("HierarchyActivity");
			solo.sleep(500);
			assertTrue("Wait for view (index: 14) failed.",
					solo.waitForView(14, 20000));
			solo.clickOnView(14);
			solo.waitForActivity("WebActivity");
			solo.sleep(3000);
			assertTrue(
					"Wait for html element - input(index: 0, id: e, name: cbox) failed.",
					solo.waitForHtmlElement("input", "e", "cbox", "", 0, 20000,
							null));
			solo.clickOnHtmlElement("input", "e", "cbox", "", 0, null);
			solo.sleep(500);
			assertTrue(
					"Wait for html element - input(index: 0, id: f, name: cbox) failed.",
					solo.waitForHtmlElement("input", "f", "cbox", "", 0, 20000,
							null));
			solo.clickOnHtmlElement("input", "f", "cbox", "", 0, null);
			solo.sleep(1000);
			assertTrue("Wait for view (index: 11) failed.",
					solo.waitForView(11, 20000));
			solo.clickOnView(11);
			solo.sleep(1000);
			assertTrue("Wait for view (index: 8) failed.",
					solo.waitForView(8, 20000));
			solo.clickOnView(8);
			solo.waitForActivity("FormsActivity");
			solo.sleep(700);
			assertTrue("Wait for view (index: 11) failed.",
					solo.waitForView(11, 20000));
			solo.clickOnView(11);
			solo.sleep(1100);
			assertTrue("Wait for list (index: 0) failed.",
					solo.waitForList(0, 20000));
			solo.clickInList(5, 0);
			solo.waitForActivity("ElementActivity");
			solo.sleep(1400);
			solo.clickOnScreen(solo.toScreenX(0.464f), solo.toScreenY(0.398f));
			solo.sleep(700);
			solo.goBack();
			solo.waitForActivity("RootActivity");
			solo.waitForActivity("HierarchyActivity");
			solo.sleep(10800);
		} catch (AssertionFailedError e) {
			solo.fail(
					"com.gorillalogic.monkeytalk.demo1.NewRobotiumTest_demo1.testRecorded_scr_fail",
					e);
			throw e;
		} catch (Exception e) {
			solo.fail(
					"com.gorillalogic.monkeytalk.demo1.NewRobotiumTest_demo1.testRecorded_scr_fail",
					e);
			throw e;
		}
	}

}
